import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Encryption1 {

    // 加密函数
    public static String encrypt(String plaintext) {
        int offset = 3;
        int length = plaintext.length();
        char[] chars = plaintext.toCharArray();
        // 加密过程（循环）
        for (int i = 0; i < length; i++) {
            chars[i] = (char) (chars[i] + i + offset);
        }

        // 改变字符串的顺序
        char firstChar = chars[0];
        char lastChar = chars[length - 1];
        chars[0] = lastChar;
        chars[length - 1] = firstChar;
        StringBuilder encrypted = new StringBuilder(new String(chars));
        encrypted.reverse();

        //返回密文
        return encrypted.toString();
    }

    // 解密函数
    public static String decrypt(String ciphertext) {
        char[] chars = new StringBuilder(ciphertext).reverse().toString().toCharArray();
        char firstChar = chars[0];
        char lastChar = chars[chars.length - 1];
        chars[0] = lastChar;
        chars[chars.length - 1] = firstChar;

        int offset = 3;
        int length = chars.length;

        // 解密过程（循环）
        for (int i = 0; i < length; i++) {
            chars[i] = (char) (chars[i] - i - offset);
        }

        //返回明文
        return new String(chars);
    }
}